﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712502_0712504_DesignPatternGame
{
    public class ProductItem : GameModel
    {
        private IDProduct _iD;
        public StateInUse StateUsed;
        public IDProduct ID
        {
            get { return _iD; }
            set { _iD = value; }
        }

        private int _timeExist;
        public int TimeExist
        {
            get { return _timeExist; }
            set { _timeExist = value; }
        }

        private int _timerNuclearAttack = 0;
        public int TimerNuclearAttack
        {
            get { return _timerNuclearAttack; }
            set { _timerNuclearAttack = value; }
        }

      
        public virtual void ItemUpdate(GameTime gameTime)
        {
            this.SpriteModel.Position = this.Position;
            this.SpriteModel.RotationAngle = this.RotationAngle;

            if (TimerNuclearAttack > 0)
            {
                TimerNuclearAttack++;
                if (TimerNuclearAttack > 5) // Delay trước khi cho nổ hết tank
                {
                    ExecuteNuclearBomb();   // Gọi hàm cho nổ bomb
                    TimerNuclearAttack = -1;
                }
            }

            if (this.StateUsed == StateInUse.Using)
            {
                TimeExist++;
                if (TimeExist > 400)    // Bien mat
                {
                    this.StateUsed = StateInUse.NotUsed;
                    TimeExist = 0;
                    return;
                }

                if (CheckPlayerTankEatProduct() == true)    // Kiểm tra tank ăn product
                {
                    ExecuteProduct();
                }
            }
        }
       
        public void RandomPosition()
        {
            Random rand = new Random();
            this.Position = new Vector2(rand.Next(200, 760), rand.Next(100, 660));          
        }

        public override void DrawToScreen(GameTime gameTime, SpriteBatch spriteBatch)
        {
            this.SpriteModel.DrawToScreen(gameTime, spriteBatch);
        }

        public bool CheckPlayerTankEatProduct()
        {
            if (GlobalVariables.CheckRectCollision(this.RectCollisionOutside, UserControlledTankManager._tankLevel.RectCollisionOutside) == true)
            {
                if (this.ID == IDProduct.Life)  // Item mạng
                {
                    UserControlledTankManager._tankLevel.Life += 1;
                }

                if (this.ID == IDProduct.Health)
                {
                    UserControlledTankManager._tankLevel.CurrentHP += 200;
                    if (UserControlledTankManager._tankLevel.CurrentHP > UserControlledTankManager._tankLevel.MaxHP)
                    {
                        UserControlledTankManager._tankLevel.CurrentHP = UserControlledTankManager._tankLevel.MaxHP;
                    }
                }

                if (this.ID == IDProduct.Star)
                {

                }

                if (this.ID == IDProduct.NuclearBomb)
                {

                }
                return true;
            }
            return false;
        }

        public void ExecuteProduct()
        {
            switch (ID)
            {
                case IDProduct.NuclearBomb:

                    break;
                case IDProduct.Lock:

                    break;
            }

            this.StateUsed = StateInUse.NotUsed;
        }

        public void ExecuteNuclearBomb()
        {
            for(int i = 0; i < AutomatedTankManager._listComTanks.Count(); i++)
            {
                AutomatedTankManager._listComTanks.ElementAt(i).CurrentHP = 0;
            }

            this.StateUsed = StateInUse.NotUsed;
            this.ID = IDProduct.Null;
        }
    }
}
